var spanNum=0; 

function accountTypeSelectOn(accountType){
	if(accountType=='0'){
		$("#evectionDiv").show();
		$("#qitaFY").hide();
		$("#othSumBtn").hide();
		$("#accountTypeS").val("差旅费");
		cancelApply('evectionDiv');
	}
	if(accountType=='1'){
		
		$("#entertainDiv").show();
		$("#qitaFY").hide();
		$("#othSumBtn").hide();
		$("#accountTypeS").val("业务招待费");
		cancelApply('entertainDiv');
	}
	if(accountType=='2'){
		if($("#qitaFY").val()=='其他'){
			$("#othSumBtn").hide();
			$("#otherCoseDiv").show();
			$("#otherTaxiDiv").hide();
			$("#accountTypeS").val("其他费用");
			$("#otherTaxiDiv").hide();
			$("#otherTaxiDiv").find("input").val("");
		}else{
			$("#otherCoseDiv").hide();
			$("#otherCoseDiv").find("input").val("");
			$("#otherTaxiDiv").show();
			$("#otherCoseDiv").hide();
			$("#accountTypeS").val("打车费");
		}
		
		cancelApply('otherCoseDiv');
	}
}

function getAccountType(type){
	var types=$("#accountTypeS").val().split("/");
	var accountType="";
	for(var i=0;i<types.length;i++){
		if(types[i]!=type){
			if(accountType==''){
				accountType=types[i];
			}else{
				accountType=accountType+"/"+types[i];
			}
		}
	}
	
	$("#accountTypeS").val(accountType);
}

function cancelApply(divType){
	if(divType=='evectionDiv'){
		$("#entertainDiv").hide();
		$("#entertainDiv").find("input").val("");
		
		$("#otherCoseDiv").hide();
		$("#otherCoseDiv").find("input").val("");
		$("#otherTaxiDiv").hide();
		$("#otherTaxiDiv").find("input").val("");
		
	}
	if(divType=='entertainDiv'){
		$("#evectionDiv").hide();
		$("#evectionDiv").find("input").val("");
		$("#evectionDiv").find("select").val("");
		
		$("#otherCoseDiv").hide();
		$("#otherCoseDiv").find("input").val("");
		$("#otherTaxiDiv").hide();
		$("#otherTaxiDiv").find("input").val("");
	}
	if(divType=='otherCoseDiv'){
		$("#evectionDiv").hide();
		$("#evectionDiv").find("input").val("");
		$("#evectionDiv").find("select").val("");
		
		$("#entertainDiv").hide();
		$("#entertainDiv").find("input").val("");
	}
	var id="";
	getAccountSum(id);
}


function insertSite(spanNum){
	var spanObject="<tr><td width=100% colspan=2>"+
				   		"出发/到达地点*：<span id='depart_city"+spanNum+"'>"+
						"<select id='province'  class='prov' style='width:108px;' ></select>"+
						"<select id='city' name='departSite' class='city required' style='width:108px;'></select>"+
						"</span>&nbsp;-&nbsp;"+
						"<span id='arrive_city"+spanNum+"'>"+
						"<select id='province'  class='prov' style='width:108px;' ></select>"+
						"<select id='city' name='arriveSite' class='city required' style='width:108px;'></select>"+
						"</span>"+
				   "</td></tr>";
	$("#newChaLvFei2 tr:eq(0)").after(spanObject);
	
}

function delApply(obj){
	$(obj).parent().parent().parent().parent().remove();
	var id="";
	getAccountSum(id);
}

function borMoneySelect(){
	var isVerBormoney = $("#isVerBormoney").val();
	if(isVerBormoney=="是"){
		$("#borrowMoneyDesc").show();
	}
	if(isVerBormoney=="否"){
		$("#borrowMoneyDesc").hide();
		$("#borrowMoneyDesc").find("input").val("");
		$("#borrowMoneyDesc").find("select").val("");
		$("#borrowMoneyId").val("");
	}
}

function getAccountSum(id){
	var accountSum=0;
	$(".sumAmount").each(function(){
		accountSum = accountSum +Number($(this).val());
	 });
	  $("#accountSum").val(Number((accountSum).toFixed(2)));
	
}


function exportClick(str){
	if(str=='show'){
		$("#CLFExport").show();
		$("#zheZC").show();
	}
	if(str=='hide'){
		$("#CLFExport").hide();
		$("#zheZC").hide();
	}
	
}

function getTimeWidget(){
	WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',isShowClear:false});
}


function getDepartSite(id){
	var cityValue = $("#"+id).val();
	var departSite="departSite";
	var departCity="departCity";
	var departCounty="departCounty";
	if(id!='departProvince'&&id!='depCity'&&id!='depCounty'){
		var num = id.substring(id.length-1,id.length);
		if(id.indexOf('Province')>0){
			departSite=departSite+num;
			departCity=departCity+num;
			departCounty=departCounty+num;
			$("#"+departSite).val(cityValue);
			$("#"+departCity).val('');
			$("#"+departCounty).val('');
		}else if(id.indexOf('City')>0){
			departCity=departCity+num;
			departCounty=departCounty+num;
			$("#"+departCity).val(cityValue);
			$("#"+departCounty).val('');
		}else if(id.indexOf('County')>0){
			departCounty=departCounty+num;
			$("#"+departCounty).val(cityValue);
		}
		
	}else{
		if(id.indexOf('Province')>0){
			$("#"+departSite).val(cityValue);
			$("#"+departCity).val('');
			$("#"+departCounty).val('');
		}else if(id.indexOf('City')>0){
			$("#"+departCity).val(cityValue);
			$("#"+departCounty).val('');
		}else if(id.indexOf('County')>0){
			$("#"+departCounty).val(cityValue);
		}
	}
	
}

function ajaxFindCityStandard(id){
	
	var city = $("#"+id).val();
	
	var subsubsidy = "subsidyStandard";
	var hotel = "hotelStandard";
	var arriveSite="arriveSite";
	var arriveCity="arriveCity";
	var arriveCounty="arriveCounty";
	var evectionDaysId="evectionDays";
	var hotelSumId ="hotelSum";
	var arriveCountryside = "arriveCountryside";
	var num ;
	if(id!='arriveProvince'&&id!='arrCity'&&id!='arrCounty'){
		num = id.substring(id.length-1,id.length);
		subsubsidy = subsubsidy+num;
		hotel = hotel+num;
		evectionDaysId = evectionDaysId+num;
		hotelSumId = hotelSumId+num;
		if(id.indexOf('Province')>0){
			arriveSite=arriveSite+num;
		}else if(id.indexOf('City')>0){
			arriveCity=arriveCity+num;
		}else if(id.indexOf('County')>0){
			arriveCounty=arriveCounty+num;
		}
	}
	if(id.indexOf('Province')>0){
		arriveCity=arriveCity+num;
		arriveCounty=arriveCounty+num
		$("#"+arriveSite).val(city);
		$("#"+arriveCity).val('');
		$("#"+arriveCounty).val('');
	}else if(id.indexOf('City')>0){
		arriveCounty=arriveCounty+num
		$("#"+arriveCity).val(city);
		$("#"+arriveCounty).val('');
	}else if(id.indexOf('County')>0){
		$("#"+arriveCounty).val(city);
		//$("#"+subsubsidy).val("80");
	}
	if(id.indexOf('County')>0){
		if($("#"+arriveCounty).val()==null || $("#"+arriveCounty).val()== ""){
			if(num == null){
				city = $("#"+arriveCity).val();
				id="arrCity";
			}else{
				city = $("#"+arriveCity+num).val();
				id="arrCity"
			}
		}
	}
	
	
	$.ajax({
		type : 'POST',  
		url : global_ctx + '/fin/accountApply/city',  
		data : 'city='+city+'&cityId='+id,  
		success : function(data) {  
			var jsonData = eval("("+data+")");
			$.each(jsonData.standard,function(idx,item){
				if(num != null){
					arriveCountryside=arriveCountryside+num;
				}
				if($("#"+arriveCounty).val()==null || $("#"+arriveCounty).val()== ""){
					$("#"+subsubsidy).val(item.food);
				}else{
					$("#"+subsubsidy).val("80");
				}
				
				//$("#"+subsubsidy).val(item.food);
				$("#"+hotel).val(item.hotel);
				if($("#"+evectionDaysId).val()!=''){
					getSubsidySum(evectionDaysId);
				}
				if($("#"+hotelSumId).val()!=''){
					checkHotelSum(hotelSumId);
				}
			})
		}  
	});
	
	/*if(id.indexOf('Province')>0){
		arriveCity=arriveCity+num;
		arriveCounty=arriveCounty+num
		$("#"+arriveSite).val(city);
		$("#"+arriveCity).val('');
		$("#"+arriveCounty).val('');
	}else if(id.indexOf('City')>0){
		arriveCounty=arriveCounty+num
		$("#"+arriveCity).val(city);
		$("#"+arriveCounty).val('');
	}else if(id.indexOf('County')>0){
		$("#"+arriveCounty).val(city);
		//$("#"+subsubsidy).val("80");
	}*/
}


function changeSite(id,city){
	$("#depart_city").citySelect({  
	    url:global_ctxStatic+"/cityselect/js/city.fin.js",  
	    prov:city, //省份 
	    city:city, //城市 
	    //dist:"岳麓区", //区县 
	    //nodata:"none", //当子集无数据时，隐藏select 
	    required:false
	});
	
	$("#arrive_city").citySelect({  
	    url:global_ctxStatic+ "/cityselect/js/city.fin.js",  
	    prov:"${customer.province}", //省份 
	    city:"${customer.arriveCity}", //城市 
	    //dist:"岳麓区", //区县 
	    //nodata:"none", //当子集无数据时，隐藏select 
	    required:false
	});
}



function checkHotelSum(id){
	var hotelSum = $("#"+id).val();
	var evectionDayId = "evectionDays";
	var hotelStandardId ="hotelStandard";
	var hotelWarnId = "hotelWarn";
	var isHotelWarnId = "isHotelWarn";
	var arriveTime = "arriveTime";
	var backDepartTime = "backDepartTime";
	var hotelDescId = "hotelDesc";
	if(id!='hotelSum'){
		var num = id.substring(id.length-1,id.length);
		evectionDayId = evectionDayId+num;
		hotelStandardId = hotelStandardId+num;
		isHotelWarnId = isHotelWarnId+num;
		hotelWarnId = hotelWarnId+num;
		arriveTime = arriveTime+num;
		backDepartTime =backDepartTime+num;
		hotelDescId = hotelDescId+num;
	}
	
	var startTime = $("#"+arriveTime).val();
	var endTime = $("#"+backDepartTime).val();
	var standard = $("#"+hotelStandardId).val();
	
	$.ajax({
		type : 'POST',  
		url : global_ctx + '/fin/accountApply/days',  
		data : 'startTime='+startTime+'&endTime='+endTime,  
		success : function(data) {  
			var days = data;
			if(standard==''){
				$.jBox.error('请选择出发到达地点！', '错误');
				$("#"+id).val('');
				return;
			}
			
			if(standard!='据实报销'){
				if(Number(hotelSum)>(Number(days)-1)*Number(standard)){
					$("#"+hotelWarnId).show();
					$("#"+isHotelWarnId).val("是");
					$("#"+hotelDescId).show();
				}else{
					$("#"+hotelWarnId).hide();
					$("#"+isHotelWarnId).val("否");
					$("#"+hotelDescId).hide();
				}
			}
		}  
	});
	
	getAccountSum(id);
}

function getBackDepartSite(id){
	var cityValue = $("#"+id).val();
	var departSite="backDepartSite";
	var departCity="backDepartCity";
	var departCounty="backDepartCounty";
	if(id!='departProvinceBack'&&id!='depCityBack'&&id!='depCountyBack'){
		var num = id.substring(id.length-1,id.length);
		if(id.indexOf('Province')>0){
			departCity=departCity+num;
			departSite=departSite+num;
			departCounty=departCounty+num;
			$("#"+departSite).val(cityValue);
			$("#"+departCity).val('');
			$("#"+departCounty).val('');
		}else if(id.indexOf('City')>0){
			departCity=departCity+num;
			departCounty=departCounty+num;
			$("#"+departCity).val(cityValue);
			$("#"+departCounty).val('');
		}else if(id.indexOf('County')>0){
			departCounty=departCounty+num;
			//alert(departCounty)
			$("#"+departCounty).val(cityValue);
			//alert($("#"+departCounty).val());
		}
		
	}else{
		if(id.indexOf('Province')>0){
			$("#"+departSite).val(cityValue);
			$("#"+departCity).val('');
			$("#"+departCounty).val('');
		}else if(id.indexOf('City')>0){
			$("#"+departCity).val(cityValue);
			$("#"+departCounty).val('');
		}else if(id.indexOf('County')>0){
			//alert($("#"+departCounty).val());
			$("#"+departCounty).val(cityValue);
		}
	}
	
}

function getBackArriveSite(id){
	var cityValue = $("#"+id).val();
	var arriveSite="backArriveSite";
	var arriveCity="backArriveCity";
	var arriveCounty="backArriveCounty";
	if(id!='arriveProvinceBack'&&id!='arrCityBack'&&id!='arrCountyBack'){
		var num = id.substring(id.length-1,id.length);
		if(id.indexOf('Province')>0){
			arriveSite=arriveSite+num;
			arriveCity=arriveCity+num;
			arriveCounty=arriveCounty+num;
			$("#"+arriveSite).val(cityValue);
			$("#"+arriveCity).val('');
			$("#"+arriveCounty).val('');
		}else if(id.indexOf('City')>0){
			arriveCity=arriveCity+num;
			arriveCounty=arriveCounty+num;
			$("#"+arriveCity).val(cityValue);
			$("#"+arriveCounty).val('');
		}else if(id.indexOf('County')>0){
			arriveCounty=arriveCounty+num;
			$("#"+arriveCounty).val(cityValue);
		}
		
	}else{
		if(id.indexOf('Province')>0){
			$("#"+arriveSite).val(cityValue);
			$("#"+arriveCity).val('');
			$("#"+arriveCounty).val('');
		}else if(id.indexOf('City')>0){
			$("#"+arriveCity).val(cityValue);
			$("#"+arriveCounty).val('');
		}else if(id.indexOf('County')>0){
			$("#"+arriveCounty).val(cityValue);
		}
	}
	
}


function timeOnchange(id){
	var evectionDaysId="evectionDays";
	var hotelSumId="hotelSum";
	if(id!='departTime'&&id!='arriveTime'&&id!='backDepartTime'&&id!='backArriveTime'){
		var num = id.substring(id.length-1,id.length);
		evectionDaysId = evectionDaysId+num;
		hotelSumId= hotelSumId+num;
	}
	
	if($("#"+evectionDaysId).val()!=''){
		$("#"+evectionDaysId).val('');
		checkHotelSum(hotelSumId);
	}
	
}

function getSubsidySum(id){
	var evectionDay = $("#"+id).val();
	var subsidyId = "subsidySum";
	var arriveSiteId = "arriveSite";
	var arriveCityId = "arriveCity";
	var StandardId = "subsidyStandard";
	var departTimeIs = "departTime";
	var backArriveTimeId = "backArriveTime";
	if(id!='evectionDays'){
		var num = id.substring(id.length-1,id.length);
		arriveSiteId = arriveSiteId+num;
		arriveCityId = arriveCityId+num;
		StandardId = StandardId+num;
		subsidyId = subsidyId+num;
		departTimeIs = departTimeIs+num;
		backArriveTimeId = backArriveTimeId+num;
	}
	var standardStr = $("#"+StandardId).val();
	var arriveSite = $("#"+arriveSiteId).val();
	var arriveCity = $("#"+arriveCityId).val();
	var startTime = $("#"+departTimeIs).val();
	var endTime = $("#"+backArriveTimeId).val();
	if(arriveSite==''&&arriveCity==''){
		$.jBox.error('请选择达到地点', '错误');
		$("#"+id).val('');
		return;
	}
	
	if(startTime==''||backArriveTimeId==''){
		$.jBox.error('请将出发与返回时间填写完整！', '错误');
		$("#"+id).val('');
		return;
	}else{
		$.ajax({
			type : 'POST',  
			url : global_ctx + '/fin/accountApply/days',  
			data : 'startTime='+startTime+'&endTime='+endTime,  
			success : function(data) {  
				var days = data;
				if(Number(evectionDay)>Number(days)){
					$.jBox.error('填写天数已经大于实际出差天数，请重新填写！', '错误');
					$("#"+id).val('');
				}else{
					if(arriveSite!=''&&arriveCity==''){
						var arriveStr = "北京;上海;深圳;天津;重庆;香港;澳门;台湾;";
						var arriveArray = arriveStr.split(";");
						var flag = false;
						for(var i =0;i<arriveArray.length;i++){
							if(arriveArray[i]!=null&&arriveArray[i]!=''&&arriveArray[i]==arriveSite){
								$("#"+subsidyId).val(standardStr*evectionDay);
								getAccountSum(id);
								flag = true;
								return;
							}
						}
						
						if(!flag){
							$.jBox.error('请填写达到城市！', '错误');
							$("#"+id).val('');
							return;
						}
						
					}
					
					if(arriveSite!=''&&arriveCity!=''){
						$("#"+subsidyId).val(standardStr*evectionDay);
						getAccountSum(id);
					}
					
				}
				
			}  
		});
	}
}

function checkInvoives(id){
	var invoivesNum = $("#"+id).val();
	var btnId = "";
	if(id.indexOf("cityInvoices")>-1){
		var citySumBtnId="citySumBtn";
		if(id!='cityInvoices'){
			var num = id.substring(id.length-1,id.length);
			citySumBtnId = citySumBtnId+num;
		}
		btnId=citySumBtnId;
	}else if(id.indexOf("originalInvoices")>-1){
		btnId="othSumBtn";
	}
	
	if(Number(invoivesNum)>2){
		$("#"+btnId).show();
	}else{
		$("#"+btnId).hide();
	}
}

